package com.gwtapps.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;


public class Hangman implements EntryPoint {

	private FlowPanel letters = new FlowPanel();
	private Label wordLabel = new Label();
	private Image image = new Image();

	private final int MAX_GUESSES = 6;
	private int misses;
	private String[] words;
	private String word;
	private char[] visibleWord;
	
	public void onModuleLoad() {
		
		//create interface
		RootPanel hangman = RootPanel.get("hangman");
		hangman.add(letters);
		hangman.add(image);
		hangman.add(wordLabel);
		
		//load words
		RequestBuilder requestBuilder = new RequestBuilder( RequestBuilder.GET, "movies.txt" );
		try {
			requestBuilder.sendRequest( null, new RequestCallback(){

				public void onError(Request request, Throwable exception) {
					GWT.log( "failed getting movie list", exception );
				}

				public void onResponseReceived(Request request, Response response) {
					words = response.getText().split("\n");
					startGame();
				}} );
		} catch (RequestException e) {
			GWT.log( "failed getting movie list", e );
		}
		
//		HTTPRequest.asyncGet("movies.txt", new ResponseTextHandler(){
//			public void onCompletion(String responseText) {
//				words = responseText.split("\n");
//				startGame();
//			}
//		});
		
		wordLabel.setStyleName("word");
	}
	
	public void startGame(){
		misses = 0;
		//add letter buttons
		letters.clear();
		for( char letter = 'A'; letter <= 'Z'; letter++ ){
			final Button button = new Button(Character.toString(letter));
			button.addClickListener( new ClickListener(){
				public void onClick( Widget sender ){
					button.setEnabled(false);
					guess( button.getText().charAt(0) );
				}
			});
			letters.add( button );
		}
		image.setUrl("hm1.gif");
		
		setWord(words[Random.nextInt(words.length)]);
	}
	
	public void setWord( String newWord ){
		word = newWord.toUpperCase();
		visibleWord = new char[word.length()];
		for( int i=0; i<word.length(); i++ ){
			if( word.charAt(i) != ' ' )
				visibleWord[i]='_';
			else
				visibleWord[i]=' ';
		}
		wordLabel.setText( new String(visibleWord) );
	}
	
	public void guess( char letter ){
		boolean badGuess = true;
		boolean wordFinished = true;
		
		//check for matches for this letter
		for( int i=0; i<word.length(); i++ ){
			if( word.charAt(i)==letter ){
				visibleWord[i] = letter;
				badGuess = false;
			}
			else if( visibleWord[i]=='_'){
				wordFinished = false;
			}
		}
		wordLabel.setText( new String(visibleWord) );
		
		if( wordFinished ){
			Window.alert( "Congratulations, you got the answer!" );
			startGame();
		}
		else if( badGuess ){
			misses++;
			image.setUrl("hm"+Integer.toString(misses+1)+".gif");
			if( misses == MAX_GUESSES ){
				wordLabel.setText( word );
				Window.alert( "You ran out of guesses! The answer is "+ word);
				startGame();
			}
		}
	}
}
